﻿
using IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class BaseDal<T>:IBaseDal<T> where T : class
    {
        MyDbContext db;
        public BaseDal(MyDbContext db)
        {
            this.db = db;
        }

        public async Task<int> AddAsync(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }

        public async Task<int> AddRangeAsync(List<T> list)
        {
            await db.Set<T>().AddRangeAsync(list);
            return await db.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(T t)
        {
            db.Set<T>().Remove(t);
            return await db.SaveChangesAsync();
        }

        public async Task<int> DeleteRangeAsync(List<T> list)
        {
            db.Set<T>().RemoveRange(list);
            return await db.SaveChangesAsync();
        }

        public IQueryable<T> GetAsync()
        {
            return db.Set<T>();
        }

        public async Task<int> UpdateAsync(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync();
        }

        public async Task<int> UpdateRangeAsync(List<T> list)
        {
            db.Set<T>().UpdateRange(list); 
            return await db.SaveChangesAsync();
        }
    }

}
